//
//  HandyMethods.m
//  liBaQiang
//
//  Created by 奇林刘 on 2018/10/9.
//  Copyright © 2018年 liu. All rights reserved.
//

#import "HandyMethods.h"
#import "LoginVC.h"
#import <UIKit/UIKit.h>

@implementation HandyMethods

+ (NSDictionary *)getPlistDicByName:(NSString *)name {
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"];
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    return dic;
}

+ (NSString *)getTimeStamp {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init].fp_dateFormat(@"yyyy-MM-dd HH:mm:ss");
    return [formatter stringFromDate:[NSDate date]];
}

+ (UIColor *)getColorWithHexadecimalString:(NSString*)string {
    NSString *cString = [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // 判断前缀
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    // 从六位数值中找到RGB对应的位数并转换
    NSRange range;
    range.location = 0;
    range.length = 2;
    //R、G、B
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

+ (NSString *)getFormattedCountStringWithCount:(NSInteger)count {
    NSString *formattedCountString;
    NSInteger tenThousands = count / 10000;
    if (tenThousands > 0) {
        formattedCountString = [NSString stringWithFormat:@"%zd万", tenThousands];
    } else {
        formattedCountString = [NSString stringWithFormat:@"%zd", count];
    }
    return formattedCountString;
}

+ (void)dispatchAfter:(CGFloat)seconds do:(dispatch_block_t)block {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
}

+ (UIViewController *)getViewControllerForView:(UIView *)view {
    for (UIView *next = [view superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

+ (int)getRandomNumber:(int)from to:(int)to {
    return (int)(from + (arc4random() % (to - from + 1))); 
}

+ (void)endEditing {
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];;
}

+ (void)reloadApp {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    UIViewController *VC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
    window.rootViewController = VC;
    [window makeKeyAndVisible];
    
    [HandyMethods dispatchAfter:0.5 do:^{
        LoginVC *loginVC = [[LoginVC alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        nav.navigationBar.translucent = NO;
        nav.navigationBar.tintColor = HexadecimalColor(@"#141414");
        
        if (!ObtainUserToken) [window.rootViewController presentViewController:nav animated:YES completion:nil];
    }];
}

@end
